//Purpose: Performs the Analysis for Crimes Against Morality ONLINE APPENDIX
//First version: September 9, 2020; Jennifer Seager (jseagermuz@gwu.edu)
//This version: September 18, 2020; Jennifer Seager (jseagermuz@gwu.edu)

clear
clear matrix
estimates clear
cap log close
set more off

#delimit;
***Setting Macros;

	****For the FSW Data;
	local sw1 = "if sw==1&dataset==1&full==1";
	*still in sex work;
	local sw2 = "if panel_v3==1&dataset==1";
	*in the panel and still in sex work;
	local sw3 = "if panelsti_sept==1&dataset==1";
	*in the September STI panel.;
	local sw4 = "if wspanel==1&dataset==1";

	***For the FSW transaction data;
	local tsw1 = "if dataset==2&full==1";
	local tsw2 = "if panel_f==1 & dataset==2&full==1";

	***For the Client Data;
	local c1 = "if dataset==3";

	***For the Client transaction data;
	local tc1 = "if dataset==4";
	
***Setting controls;
local char = 
	"divwid unmarried edyrs age children sw_locyrs disc_factor risktol_nodl nodlr ";
local tchar1 = "regular outsider clean handsome old rich";
local tchar2 = "regular";
local cchar = "divwid unmarried edyrs age disc_factor risktol";
local tcchar = "regular clean attractive";

use "$da/Crimes-Against-Morality_Main_data-constructed.dta", clear;
***BEGINNING ANALYSIS;

***Table I.I;
gen count = 1 ;
replace count = . if married==. & wave==1; 
	//restricting baseline to those who completed the baseline survey;
	
*Showing the tabulations that create the counts.;
*Sex Worker Data;
*Baseline sample;
tab count if dataset==1 & wave==1& closing==1;
tab count if dataset==1 & wave==1& closing==0&formal==1;
tab count if dataset==1 & wave==1& closing==0;
tab count if dataset==1 & wave==1& formal==1;
tab count if dataset==1 & wave==1;

*Endline Sample;
tab count if dataset==1 & wave==2 &closing==1;
tab count if dataset==1 & wave==2& closing==0&formal==1;
tab count if dataset==1 & wave==2& closing==0;
tab count if dataset==1 & wave==2& formal==1;
tab count if dataset==1 & wave==2;

*Endline Sample, In Person;
tab count if dataset==1 & wave==2 &closing==1&quest=="F";
tab count if dataset==1 & wave==2& closing==0&formal==1&quest=="F";
tab count if dataset==1 & wave==2& closing==0&quest=="F";
tab count if dataset==1 & wave==2& formal==1&quest=="F";
tab count if dataset==1 & wave==2&quest=="F";

*Endline Sample, In Person;
tab count if dataset==1 & wave==2 &closing==1&quest=="P";
tab count if dataset==1 & wave==2& closing==0&formal==1&quest=="P";
tab count if dataset==1 & wave==2& closing==0&quest=="P";
tab count if dataset==1 & wave==2& formal==1&quest=="P";
tab count if dataset==1 & wave==2&quest=="P";

*Endline Sample, Informant;
tab count if dataset==1 & wave==2 &closing==1&quest=="I";
tab count if dataset==1 & wave==2& closing==0&formal==1&quest=="I";
tab count if dataset==1 & wave==2& closing==0&quest=="I";
tab count if dataset==1 & wave==2& formal==1&quest=="I";
tab count if dataset==1 & wave==2&quest=="I";

*Endline Panel Sample;
tab count if dataset==1 & wave==1& closing==1 &followed==1;
tab count if dataset==1 & wave==1& closing==0&formal==1&followed==1;
tab count if dataset==1 & wave==1& closing==0&followed==1;
tab count if dataset==1 & wave==1& formal==1&followed==1;
tab count if dataset==1 & wave==1& followed==1;

*Endline Panel Sample, In person;
tab count if dataset==1 & wave==2& closing==1 &followed==1&quest=="F";
tab count if dataset==1 & wave==2& closing==0 &formal==1&followed==1&quest=="F";
tab count if dataset==1 & wave==2& closing==0 &followed==1&quest=="F";
tab count if dataset==1 & wave==2& formal==1 &followed==1&quest=="F";
tab count if dataset==1 & wave==2& followed==1&quest=="F";

*Endline Panel Sample, By phone;
tab count if dataset==1 & wave==2& closing==1 &followed==1&quest=="P";
tab count if dataset==1 & wave==2& closing==0 &formal==1&followed==1&quest=="P";
tab count if dataset==1 & wave==2& closing==0 &followed==1&quest=="P";
tab count if dataset==1 & wave==2& formal==1 &followed==1&quest=="P";
tab count if dataset==1 & wave==2& followed==1&quest=="P";

*Endline Panel Sample, By Informant;
tab count if dataset==1 & wave==2& closing==1 &followed==1&quest=="I";
tab count if dataset==1 & wave==2& closing==0 &formal==1&followed==1&quest=="I";
tab count if dataset==1 & wave==2& closing==0 &followed==1&quest=="I";
tab count if dataset==1 & wave==2& formal==1 &followed==1&quest=="I";
tab count if dataset==1 & wave==2& followed==1&quest=="I";

*Endline Analysis Sample;
tab count if dataset==1 & wave==2 &closing==1&full==1&sw==1;
tab count if dataset==1 & wave==2& closing==0&formal==1&full==1&sw==1;
tab count if dataset==1 & wave==2& closing==0&full==1&sw==1;
tab count if dataset==1 & wave==2& formal==1&full==1&sw==1;
tab count if dataset==1 & wave==2&full==1&sw==1;

*Endline Analysis Sample, Panel Sample;
tab count if dataset==1 & wave==2 &closing==1&full==1&sw==1&panel_v3==1;
tab count if dataset==1 & wave==2& closing==0&formal==1&full==1&sw==1&panel_v3==1;
tab count if dataset==1 & wave==2& closing==0&full==1&sw==1&panel_v3==1;
tab count if dataset==1 & wave==2& formal==1&full==1&sw==1&panel_v3==1;
tab count if dataset==1 & wave==2&full==1&sw==1&panel_v3==1;

*Endline Analysis Sample, Not in sex work, by phone and in person, panel;
tab count if dataset==1 & wave==2 &closing==1&followed==1&sw==0&(quest=="F" | quest=="P");
tab count if dataset==1 & wave==2& closing==0&followed==1&formal==1&sw==0&(quest=="F" | quest=="P");
tab count if dataset==1 & wave==2& closing==0&followed==1&sw==0&(quest=="F" | quest=="P");
tab count if dataset==1 & wave==2& formal==1&followed==1&sw==0&(quest=="F" | quest=="P");
tab count if dataset==1 & wave==2&sw==0&followed==1&(quest=="F" | quest=="P");

*Health Data;
drop count;
gen count = 1;
*Baseline;
tab count if wave==1 & sept_any!=.&closing==1;
tab count if wave==1 & sept_any!=.&closing==0 & formal==1;
tab count if wave==1 & sept_any!=.&closing==0 ;
tab count if wave==1 & sept_any!=. & formal==1;
tab count if wave==1 & sept_any!=. ;

*Endline;
tab count if wave==2 & sept_any!=.&closing==1;
tab count if wave==2 & sept_any!=.&closing==0 & formal==1;
tab count if wave==2 & sept_any!=.&closing==0 ;
tab count if wave==2 & sept_any!=. & formal==1;
tab count if wave==2 & sept_any!=. ;

*Panel;
tab count if wave==2 & sept_any!=.&closing==1&panelsti_sept==1;
tab count if wave==2 & sept_any!=.&closing==0 & formal==1&panelsti_sept==1;
tab count if wave==2 & sept_any!=.&closing==0 &panelsti_sept==1;
tab count if wave==2 & sept_any!=. & formal==1&panelsti_sept==1;
tab count if wave==2 & sept_any!=. &panelsti_sept==1;

*Client Data;
*Baseline;
tab count if dataset==3 & wave==1 & closing==1;
tab count if dataset==3 & wave==1 & closing==0&formal==1;
tab count if dataset==3 & wave==1 & closing==0;
tab count if dataset==3 & wave==1 & formal==1;
tab count if dataset==3 & wave==1;

*Endline;
tab count if dataset==3 & wave==2 & closing==1;
tab count if dataset==3 & wave==2 & closing==0&formal==1;
tab count if dataset==3 & wave==2 & closing==0;
tab count if dataset==3 & wave==2 & formal==1;
tab count if dataset==3 & wave==2;

*Locations of sex workers if no longer in sex work at endline;
*Number of baseline sex workers no longer in sex work;
tab count if dataset==1 & wave==2 & sw==0&followed==1&closing==1;
tab count if dataset==1 & wave==2 & sw==0&followed==1&closing==0&formal==1;
tab count if dataset==1 & wave==2 & sw==0&followed==1&closing==0;
tab count if dataset==1 & wave==2 & sw==0&followed==1&formal==1;
tab count if dataset==1 & wave==2 & sw==0&followed==1;

*Where did people go if no longer in sex work;

tab move if dataset==1 & wave==2 & sw==0 & followed==1 & closing==1;
tab move if dataset==1 & wave==2 & sw==0 & followed==1 & closing==0&formal==1;
tab move if dataset==1 & wave==2 & sw==0 & followed==1 & closing==0;
tab move if dataset==1 & wave==2 & sw==0 & followed==1 & formal==1;
tab move if dataset==1 & wave==2 & sw==0 & followed==1 ;


*Occupations of sex workers;
*Sex work;
tab count if dataset==1 & wave==2 & sw==1 & followed==1 & closing==1;
tab count if dataset==1 & wave==2 & sw==1 & followed==1 & closing==0&formal==1;
tab count if dataset==1 & wave==2 & sw==1 & followed==1 & closing==0;
tab count if dataset==1 & wave==2 & sw==1 & followed==1 & formal==1;
tab count if dataset==1 & wave==2 & sw==1 & followed==1 ;

*Other worker;

tab worktype if dataset==1 & wave==2 & followed==1 & closing==1;
tab worktype if dataset==1 & wave==2 & followed==1 & closing==0&formal==1;
tab worktype if dataset==1 & wave==2 & followed==1 & closing==0;
tab worktype if dataset==1 & wave==2 & followed==1 & formal==1;
tab worktype if dataset==1 & wave==2 & followed==1 ;

***Table I.II;

	***See Separate do file with Susenas Data analysis;

***Table I.III;

eststo form: mean married divwid unmarried edyrs age children sw_locyrs disc_factor 
				  hlthacc condeasy havecondom easyask  
				  if wave==1&dataset==1&formal==1, cluster(wsid);
eststo nonform: mean married divwid unmarried edyrs age children sw_locyrs disc_factor 
				  hlthacc condeasy havecondom easyask 
				  if wave==1&dataset==1&formal==0, cluster(wsid);
				  
eststo form1: mean risktol if wave==1&dataset==1&formal==1, cluster(wsid);
eststo nonform1: mean risktol if wave==1&dataset==1&formal==0, cluster(wsid);
				  
eststo form2: mean sept_any if wave==1&dataset==1&formal==1, cluster(wsid);
eststo nonform2: mean sept_any if wave==1&dataset==1&formal==0, cluster(wsid);

eststo form3: mean cndmpr if wave==1&dataset==1&formal==1, cluster(wsid);
eststo nonform3: mean cndmpr if wave==1&dataset==1&formal==0, cluster(wsid);

eststo form4: mean noco1_mean if wave==1&dataset==1&formal==1, cluster(wsid);
eststo nonform4: mean noco1_mean if wave==1&dataset==1&formal==0, cluster(wsid);


eststo form_cl: mean married divwid unmarried edyrs age disc_factor risktol 
				if wave==1&dataset==3&formal==1, cluster(wsid);
eststo nonform_cl: mean married divwid unmarried edyrs age disc_factor risktol 
				   if wave==1&dataset==3&formal==0, cluster(wsid);
eststo form_cl2: mean noco11 if wave==1&dataset==3&formal==1, cluster(wsid);
eststo nonform_cl2: mean noco11 if wave==1&dataset==3&formal==0, cluster(wsid);

esttab form nonform
	using "$dout/Table_I-III.tex",
	collabels(none) obslast cells(b(fmt(3)) se(par fmt(3)))
	title("A. Female Sex Workers")
	labcol2("Married" "Divorced or Widowed" "Never Married" "Years of Education" 
			"Age" "Children" "Years at Location" "Discount Factor"
			"Health Exam" "Easy to Obtain" "Has Condom"
			"Easy to Ask" "Sample Size")
	mlabels("Formal" "Informal")
	replace nogaps nostar;
	
esttab form1 nonform1
	using "$dout/Table_I-III.tex",
	collabels(none) obslast cells(b(fmt(3)) se(par fmt(3)))
	labcol2("Risk Tolerance")
	mlabels("Formal" "Informal") nonumbers
	append nogaps nostar noobs;
	
esttab form2 nonform2
	using "$dout/Table_I-III.tex",
	collabels(none) obslast cells(b(fmt(3)) se(par fmt(3)))
	labcol2("Biological Tests")
	mlabels("Formal" "Informal") nonumbers
	append nogaps nostar noobs;
esttab form3 nonform3
	using "$dout/Table_I-III.tex",
	collabels(none) obslast cells(b(fmt(3)) se(par fmt(3)))
	labcol2("Condom Price")
	mlabels("Formal" "Informal") nonumbers
	append nogaps nostar noobs;
esttab form4 nonform4
	using "$dout/Table_I-III.tex",
	collabels(none) obslast cells(b(fmt(3)) se(par fmt(3)))
	labcol2("No Condom Used")
	mlabels("Formal" "Informal") nonumbers
	append nogaps nostar noobs;

esttab form_cl nonform_cl
	using "$dout/Table_I-III.tex",
	collabels(none) obslast cells(b(fmt(3)) se(par fmt(3)))
	title("B. Clients")
	labcol2("Married" "Divorced or Widowed" "Never Married" "Years of Education" 
			"Age" "Discount Factor" "Risk Tolerance" "No Condom Used" "Sample Size")
	mlabels("Formal" "Informal")
	append nogaps nostar;
	
	esttab form_cl2 nonform_cl2
	using "$dout/Table_I-III.tex",
	collabels(none) obslast cells(b(fmt(3)) se(par fmt(3)))
	title("B. Clients")
	labcol2("No Condom Used" "Sample Size")
	mlabels("Formal" "Informal")
	append nogaps nostar;
estimates clear; eststo clear;	
	
	
	local i=1;
foreach var in married divwid unmarried edyrs age children sw_locyrs disc_factor risktol 
				sept_any hlthacc condeasy havecondom easyask cndmpr noco1_mean{;

reg `var' formal if wave==1&dataset==1, cluster(wsid);
estimates store reg`i';
local i=`i'+1;
};
*;


esttab reg* using "$dout/Table_I-III.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3))  p(fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Sex Worker Differences by Formal/Informal)
			 append;
estimates clear;
eststo clear;

	local i=1;
foreach var in married divwid unmarried edyrs age disc_factor risktol noco11 {;

reg `var' formal if wave==1&dataset==3, cluster(wsid);
estimates store reg`i';
local i=`i'+1;
};
*;

esttab reg* using "$dout/Table_I-III.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3))  p( fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Client Differences by Formal/Informal)
			 append;
estimates clear;
eststo clear;

***Table I.IV;
*Formal Worksites only;
areg sept_any 	dd_cl el if dataset==1&formal==1, cluster(wsid) abs(wsid);
	sum sept_any if dataset==1&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg1;
areg hlthacc 	dd_cl el `char' `sw1'& condeasy!=.&formal==1, cluster(wsid) abs(wsid);
	sum hlthacc `sw1'&condeasy!=.&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg2;
areg cndom_index 	dd_cl el `char' `sw1'& formal==1, cluster(wsid) abs(wsid);
	sum cndom_index `sw1'&condeasy!=.&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg_index;
areg condeasy 	dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
	sum condeasy `sw1'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg3;
areg havecondom dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
	sum havecondom `sw1'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg4;
areg condomprice dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
	sum cndmpr `sw1'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg5;
areg easyask dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
	sum easyask `sw1'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg6;
areg noco1 		dd_cl el `tchar1' `tsw1'&formal==1, cluster(wsid) abs(fsw_fe);
	sum noco1 `tsw1'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=1;
	estimate store reg7;
areg noco1 		dd_cl el `tcchar' `tc1'&formal==1, cluster(wsid) abs(cl_fe);
	sum noco1 `tc1'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=1;
	estimate store reg8;

esttab reg1 reg2 reg_index reg3 reg4 reg5 reg6 reg7 reg8 using "$dout/Table_I-IV.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Formal Worksites)
			 keep(dd_cl el divwid unmarried edyrs age children 
				  sw_locyrs disc_factor risktol_nodl regular outsider clean 
				  handsome attractive old rich )
			 scalars(bl_crim ws_fe ind_fe)
			 append;
	
estimates clear;
eststo clear;


*Formal Worksites, panel;
areg sept_any 	dd_cl el `sw3'&formal==1, cluster(wsid) abs(wsid);
	sum sept_any `sw3'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg1;
areg hlthacc 	dd_cl el `char' `sw2' &condeasy!=.&formal==1, cluster(wsid) abs(wsid);
	sum hlthacc `sw2'&condeasy!=.&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg2;
areg cndom_indexp 	dd_cl el `char' `sw2' &condeasy!=.&formal==1, cluster(wsid) abs(wsid);
	sum cndom_indexp `sw2'&condeasy!=.&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg_index;
areg condeasy 	dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
	sum condeasy `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg3;
areg havecondom dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
	sum havecondom `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg4;
areg condomprice dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
	sum cndmpr `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg5;
areg easyask	dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
	sum easyask `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg6;
areg noco1 		dd_cl el `tchar1' `tsw2'&formal==1, cluster(wsid) abs(fsw_fe);
	sum noco1 `tsw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=1;
	estimate store reg7;

esttab reg1 reg2 reg_index reg3 reg4 reg5 reg6 reg7 using "$dout/Table_I-IV.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Formal Worksites, Panel Sample)
			 keep(dd_cl el divwid unmarried edyrs age children 
				  sw_locyrs disc_factor risktol_nodl regular outsider clean 
				  handsome old rich )
			 scalars(bl_crim ws_fe ind_fe)
			 append;
	
estimates clear;
eststo clear;


*All Worksites, panel with FE;
areg sept_any 	dd_cl el `sw3', cluster(wsid) abs(fe_id);
	sum sept_any `sw3'&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg1;
areg hlthacc 	dd_cl el `sw2'&condeasy!=., cluster(wsid) abs(fe_id);
	sum hlthacc `sw2'&condeasy!=.&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg2;
areg cndom_indexp 	dd_cl el `sw2'&condeasy!=., cluster(wsid) abs(fe_id);
	sum cndom_indexp `sw2'&condeasy!=.&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg_index;
areg condeasy 	dd_cl el `sw2', cluster(wsid) abs(fe_id);
	sum condeasy `sw2'&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg3;
areg havecondom dd_cl el `sw2', cluster(wsid) abs(fe_id);
	sum havecondom `sw2'&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg4;
areg condomprice dd_cl el `sw2', cluster(wsid) abs(fe_id);
	sum cndmpr `sw2'&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg5;
areg easyask dd_cl el `sw2', cluster(wsid) abs(fe_id);
	sum easyask `sw2'&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg6;
areg noco1 		dd_cl el `tchar1' `tsw2', cluster(wsid) abs(fsw_fe);
	sum noco1 `tsw2'&el==0&closing==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=1;
	estimate store reg7;


esttab reg1 reg2 reg_index reg3 reg4 reg5 reg6 reg7 using "$dout/Table_I-IV.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(All Worksites, Panel Sample, Indiv FE)
			 keep(dd_cl el regular outsider clean handsome old rich )
			 scalars(bl_crim ws_fe ind_fe)
			 append;
	
estimates clear;
eststo clear;

*Formal Worksites, panel with FE;
areg sept_any 	dd_cl el `sw3'&formal==1, cluster(wsid) abs(fe_id);
	sum sept_any `sw3'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg1;
areg hlthacc 	dd_cl el `sw2'&condeasy!=.&formal==1, cluster(wsid) abs(fe_id);
	sum hlthacc `sw2'&condeasy!=.&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg2;
areg cndom_indexp 	dd_cl el `sw2'&condeasy!=.&formal==1, cluster(wsid) abs(fe_id);
	sum cndom_indexp `sw2'&condeasy!=.&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg_index;
areg condeasy 	dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
	sum condeasy `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg3;
areg havecondom dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
	sum havecondom `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg4;
areg condomprice dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
	sum cndmpr `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg5;
areg easyask	dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
	sum easyask `sw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=1;
	estadd scalar ind_fe=0;
	estimate store reg6;
areg noco1 		dd_cl el `tchar1' `tsw2'&formal==1, cluster(wsid) abs(fsw_fe);
	sum noco1 `tsw2'&el==0&closing==1&formal==1;
	estadd scalar bl_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=1;
	estimate store reg7;


esttab reg1 reg2 reg_index reg3 reg4 reg5 reg6 reg7 using "$dout/Table_I-IV.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Formal Worksites, Panel Sample, Indiv FE)
			 keep(dd_cl el regular outsider clean 
				  handsome old rich )
			 scalars(bl_crim ws_fe ind_fe)
			 append;
	
estimates clear;
eststo clear;

***Table I.V;
*Formal Only;
eststo: areg wrksite_index dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
eststo: areg cl7day dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
eststo: areg tr7day dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
eststo: areg hr7day dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);
eststo: areg transprice dd_cl el `tchar1' `tsw1'&formal==1, cluster(wsid) abs(fsw_fe);
eststo: areg earn7day dd_cl el `char' `sw1'&formal==1, cluster(wsid) abs(wsid);

esttab using "$dout/Table_I-V.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Formal Worksites)
			 keep(dd_cl el divwid unmarried edyrs age children 
				  sw_locyrs disc_factor risktol_nodl
				  regular outsider clean handsome old rich)
			 scalars(r2 ar2)
			 append;
	
estimates clear;
eststo clear;

*Formal Only, panel;
eststo: areg wrksite_indexp dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
eststo: areg cl7day dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
eststo: areg tr7day dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
eststo: areg hr7day dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);
eststo: areg transprice dd_cl el `tchar1' `tsw2'&formal==1, cluster(wsid) abs(fsw_fe);
eststo: areg earn7day dd_cl el `char' `sw2'&formal==1, cluster(wsid) abs(wsid);

esttab using "$dout/Table_I-V.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Formal Worksites, Panel)
			 keep(dd_cl el divwid unmarried edyrs age children 
				  sw_locyrs disc_factor risktol_nodl
				  regular outsider clean handsome old rich )
			 scalars(r2 ar2)
			 append;
	
estimates clear;
eststo clear;

*CLIENTS Formal Only;
eststo: areg wrksitec_index dd_cl closing el `cchar' `c1'&formal==1, cluster(wsid) abs(ctypeid);
eststo: areg fsws2 dd_cl closing el `cchar' `c1'&formal==1, cluster(wsid) abs(ctypeid);
eststo: areg sextimes dd_cl closing el `cchar' `c1'&formal==1, cluster(wsid) abs(ctypeid);
eststo: areg transprice dd_cl closing el `tcchar' `tc1'&formal==1, cluster(wsid) abs(cl_fe);
eststo: areg spend7day dd_cl closing el `cchar' `c1'&formal==1, cluster(wsid) abs(ctypeid);


esttab using "$dout/Table_I-V.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Formal Worksites)
			 keep(dd_cl el divwid unmarried edyrs age disc_factor risktol
				  regular clean attractive)
			 scalars(r2 ar2)
			 append;

estimates clear;
eststo clear;


*All Worksites, panel WITH FE;
eststo: areg wrksite_indexp dd_cl el `sw2', cluster(wsid) abs(fe_id);
eststo: areg cl7day dd_cl el `sw2', cluster(wsid) abs(fe_id);
eststo: areg tr7day dd_cl el `sw2', cluster(wsid) abs(fe_id);
eststo: areg hr7day dd_cl el `sw2', cluster(wsid) abs(fe_id);
eststo: areg transprice dd_cl el `tchar1' `tsw2', cluster(wsid) abs(fsw_fe);
eststo: areg earn7day dd_cl el `sw2', cluster(wsid) abs(fe_id);

esttab using "$dout/Table_I-V.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(All Worksites, panel with individual fe)
			 keep(dd_cl el regular outsider clean handsome old rich)
			 scalars(r2 ar2)
			 append;
	
estimates clear;
eststo clear;

*Formal Only, panel WITH FE;
eststo: areg wrksite_indexp dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
eststo: areg cl7day dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
eststo: areg tr7day dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
eststo: areg hr7day dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);
eststo: areg transprice dd_cl el `tchar1' `tsw2'&formal==1, cluster(wsid) abs(fsw_fe);
eststo: areg earn7day dd_cl el `sw2'&formal==1, cluster(wsid) abs(fe_id);

esttab using "$dout/Table_I-V.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(Formal Worksites, Panel with individual FE)
			 keep(dd_cl el regular outsider clean handsome old rich )
			 scalars(r2 ar2)
			 append;
	
estimates clear;
eststo clear;

***Table I.VI;

preserve;

global health0_v2 = "sept_any0_v2 sept_any20_v2 sept_any30_v2 sept_any36_v2 sept_any40_v2 sept_any505_v2 ";
global health1 = "hlthacc condeasy havecondom condomprice";


*Filling in Zeros for "not in Sex Work Sample";

foreach out in $health1 {;

replace `out' = 0 if `out'==. & wave==2 &sw==0&dataset==1;

};
*;

gen sept_any0_v2 = sept_any;
replace sept_any0_v2 = 0 if sept_any==.&wave==2&sw==0&dataset==1&tested_at_baseline==1;

gen sept_any36_v2 = sept_any;
replace sept_any36_v2 = 0.364 if sept_any==.&wave==2&sw==0&dataset==1&tested_at_baseline==1;

gen sept_any30_v2 = sept_any;
replace sept_any30_v2 = 0.30 if sept_any==.&wave==2&sw==0&dataset==1&tested_at_baseline==1;

gen sept_any20_v2 = sept_any;
replace sept_any20_v2 = 0.20 if sept_any==.&wave==2&sw==0&dataset==1&tested_at_baseline==1;

gen sept_any40_v2 = sept_any;
replace sept_any40_v2 = 0.40 if sept_any==.&wave==2&sw==0&dataset==1&tested_at_baseline==1;

gen sept_any505_v2 = sept_any;
replace sept_any505_v2 = 0.505 if sept_any==.&wave==2&sw==0&dataset==1&tested_at_baseline==1;



foreach out in $health0_v2{;

eststo hlth1_`out': areg `out' dd_cl el if dataset==1&married!=.&condeasy!=., abs(wsid) cluster(wsid);
sum `out' if wave==1&closing==0;
estadd scalar outmean = r(mean);

};
*;

esttab hlth1_*
using "$dout/Table_I-VI.tex",
	   cells(b(star fmt(3)) se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title("Impact of Criminalization on Sex Worker Health and Condom Use, replacing missing with zeroes")
			 scalars(outmean)
			 replace;
			 estimates clear; eststo clear;


restore;

***Table I.VII;

********************;
*3.1. For Main Paper;
********************;

*All Worksites, raw dd;
reg sept_any 	dd_cl el closing if dataset==1, cluster(wsid);

sum sept_any if dataset==1&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum sept_any if dataset==1&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum sept_any if dataset==1&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum sept_any if dataset==1&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg1;

reg hlthacc 	dd_cl el closing `sw1'& condeasy!=., cluster(wsid);

sum hlthacc if dataset==1&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum hlthacc if dataset==1&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum hlthacc if dataset==1&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum hlthacc if dataset==1&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg2;

reg cndom_index 	dd_cl el closing `sw1'& condeasy!=., cluster(wsid);

sum cndom_index if dataset==1&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum cndom_index if dataset==1&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum cndom_index if dataset==1&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum cndom_index if dataset==1&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg_index;


reg condeasy 	dd_cl el closing  `sw1', cluster(wsid);

sum condeasy if dataset==1&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum condeasy if dataset==1&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum condeasy if dataset==1&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum condeasy if dataset==1&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg3;

reg havecondom	dd_cl el closing  `sw1', cluster(wsid);

sum havecondom if dataset==1&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum havecondom if dataset==1&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum havecondom if dataset==1&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum havecondom if dataset==1&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg4;

reg condomprice dd_cl el closing  `sw1', cluster(wsid);
sum cndmpr if dataset==1&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum cndmpr if dataset==1&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum cndmpr if dataset==1&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum cndmpr if dataset==1&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg5;


reg easyask 	dd_cl el closing  `sw1', cluster(wsid);
sum easyask if dataset==1&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum easyask if dataset==1&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum easyask if dataset==1&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum easyask if dataset==1&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg6;

areg noco1 		dd_cl el closing `tsw1', cluster(wsid) abs(fsw_fe);
sum noco1 if dataset==2&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum noco1 if dataset==2&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum noco1 if dataset==2&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum noco1 if dataset==2&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg7;

areg noco1 		dd_cl el closing `tc1', cluster(wsid) abs(cl_fe) ;
sum noco1 if dataset==4&el==0&closing==0;
estadd scalar bl_nocrim=r(mean);
sum noco1 if dataset==4&el==0&closing==1;
estadd scalar bl_crim=r(mean);
sum noco1 if dataset==4&el==1&closing==0;
estadd scalar el_nocrim =r(mean);
sum noco1 if dataset==4&el==1&closing==1;
estadd scalar el_crim=r(mean);
	estadd scalar ws_fe=0;
	estadd scalar ind_fe=0;
estimates store reg8;


esttab reg1 reg2 reg_index reg3 reg4 reg5 reg6 reg7 reg8 using "$dout/Table_I-VII.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 title(All Worksites)
			 keep(dd_cl el closing )
			 scalars(N bl_nocrim bl_crim el_nocrim el_crim ws_fe ind_fe)
			 replace;
	
estimates clear;
eststo clear;

***Table I.VIII;

preserve;

keep if dataset==1;
keep uid wave dd_cl el divwid unmarried edyrs age children sw_locyrs disc_factor
	 risktol_nodl nodlr full sw wsid quest;
tempfile swdata;
save `swdata';

restore;
preserve;

keep if dataset==2;
keep uid wave noco1 regular clean outsider handsome old rich;
merge m:1 uid wave using `swdata';

*I am dropping the observations that od not have baseline data because they 
*entered our sample during the relisting in august/september;
drop if wave==1&_merge==2;

*at endline, did not do the transaction data with informants;
drop if wave==2&quest=="I"&_merge==2;

*did not get transaction data if the SW was not in sex work any longer;
drop if wave==2 & _merge==2 & sw==0;

*Now we are left with 13 people who did the full survey but who did not provide
*endline transaction data.;
replace noco1=0 if noco1==.;

eststo: reg noco1 divwid unmarried edyrs age children sw_locyrs disc_factor 
				   risktol_nodl nodlr regular outsider clean handsome old rich el 
				   i.wsid if full==1&sw==1, cluster(wsid);
			 
restore;
preserve;

keep if dataset==3;
keep uid wave divwid unmarried edyrs age disc_factor risktol wsid el dd_cl 
	 closing wsid;
tempfile cldata;
save `cldata';

restore;
preserve;

keep if dataset==4;
keep uid wave noco1 regular clean attractive;
merge m:1 uid wave using `cldata';

local cchar = "divwid unmarried edyrs age disc_factor risktol";
local tcchar = "regular clean attractive";

eststo: reg noco1  `cchar' `tcchar' el i.wsid , cluster(wsid);

esttab using "$dout/Table_I-VIII.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 scalars(r2 ar2)
			 replace;
			 	 
estimates clear;
eststo clear;

restore;

***Table I.IX;

preserve;

use "$da/Crimes-Against-Morality_attrition-data.dta", clear;		   
			   
eststo: xi: areg attrit_ansamp
					   c.married#closing 
					   c.edyrs#closing 
					   c.age#closing 
					   c.children#closing 
					   c.sw_locyrs#closing 
					   c.disc_factor#closing 
					   c.risktol_nodl#closing 
					   c.nodlr#closing 
					    
					   married 
					   edyrs 
					   age 
					   children 
					   sw_locyrs 
					   disc_factor 
					   risktol_nodl 
					   nodlr
					   closing, 
					   cluster(wsid) abs(wsid);

eststo: xi: areg attrit_ansamp c.hlthacc#closing 
					   c.condeasy#closing 
					   c.havecondom#closing 
					   c.condomprice#closing
					   c.cl7day#closing
					   c.tr7day#closing
					   c.hr7day#closing 
					   c.earn7day#closing 
					   
					   hlthacc
					   condeasy 
					   havecondom
					   condomprice 
					   cl7day
					   tr7day
					   hr7day
					   earn7day closing, 
					   cluster(wsid) abs(wsid);
					   
					   
eststo xi: areg attrit_sti sept_any1 c.sept_any1#closing closing, cluster(wsid)
				 abs(wsid);
				 				 
esttab using "$dout/Table_I-IX.tex", 
			 cells(b(star fmt(3)) 
			 se(par fmt(3)))
			 varwidth(14)
			 star(* 0.10 ** 0.05 *** 0.01)
			 scalars(r2 ar2)
			 replace;
			 
estimates clear;
eststo clear;

***Table I.X;


gen t = attrit_ansamp==1;
foreach x in sept_any1 hlthacc cndom_index {;
eststo: reg `x' if closing==1&attrit_ansamp==0 , cluster(wsid);
eststo: reg `x' if closing==1&attrit_ansamp==1 , cluster(wsid);
eststo: reg `x' t if closing==1, cluster(wsid);

eststo: reg `x' if closing==0&attrit_ansamp==0 , cluster(wsid);
eststo: reg `x' if closing==0&attrit_ansamp==1 , cluster(wsid);
eststo: reg `x' t if closing==0, cluster(wsid);


esttab using "$dout/Table_I-X.tex", 
	star(* 0.10 ** 0.05 *** 0.01) 
	mtitles( "Crim+No Attrit" "Crim+No Attrit" "Crim"
			 "Non-Crim+No Attrit" "Non-Crim+Attrit" "Non-Crim")  collabels(none)  
	cells(b(star fmt(3)) se(par fmt(3))) label style(tab) append;

estimates clear;
eststo clear;

};

restore;


***Table III.I;
	//this gives code to produce numbers in Appendix Table III.I that come 
	//from our FSW data;

*B. Infected;
sum sept_any if wave==1;
sum sept_any if wave==2;

*C. client condom use;
sum nevercondom if wave==1;
sum nevercondom if wave==2;

*D. having a partner and condom use;

sum client_partner if nevercondom==1&wave==1;
sum client_partner if nevercondom==1&wave==2;
sum cl_partnercondom if wave==1 & nevercondom==1;
sum cl_partnercondom if wave==2 & nevercondom==1;


sum client_partner if nevercondom==0&wave==1;
sum client_partner if nevercondom==0&wave==2;
sum cl_partnercondom if wave==1 & nevercondom==0;
sum cl_partnercondom if wave==2 & nevercondom==0;

sum sp_partner if wave==1;
sum sp_partner if wave==2;
sum sp_partnercondom if wave==1;
sum sp_partnercondom if wave==2;




